import axios from "axios";
import qs from "qs";

import {
    Message
} from 'element-ui'

let baseURL = 'http://m.huangxh.top'
axios.defaults.baseURL = baseURL
axios.defaults.timeout = 12000
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8;';

axios.interceptors.request.use(config => {
    return config;
}, error => {
    return Promise.reject(error);
})
axios.interceptors.response.use(response => {
    return response;
}, error => {
    Message.error("网络请求错误，请稍后再试")
    return Promise.reject(error);
});

export default {
    get (url, params = {}) {
        return new Promise((resolve, reject) => {
            axios.get(url, {
                params
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    post (url, params = {}) {
        return new Promise((resolve, reject) => {
            axios.post(url, qs.stringify(params))
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
}